//
//  FZDialogView.m
//  EnglishTalk
//
//  Created by CyonLeuMBP on 2017/2/21.
//  Copyright © 2017年 Feizhu Tech. All rights reserved.
//

#import "FZDialogView.h"
#import "FZStyleSheet.h"
#import <Masonry.h>
#import "SRCommonHeader.h"

@interface FZDialogView ()

@property (strong, nonatomic) UIImage *iconImage;
@property (strong, nonatomic) UITapGestureRecognizer *tapBgGesture;

@end

@implementation FZDialogView

- (instancetype)initWithStyle:(FZDialogViewStyle)viewStyle{
    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if (self) {
        [self setupView];
    }
    
    return self;
}


- (void)setupView {
    FZStyleSheet *css = [FZStyleSheet currentStyleSheet];
    
    self.contentView = [[UIView alloc] init];
    self.contentView.layer.masksToBounds = YES;
    self.contentView.layer.cornerRadius = 5;
    
    self.closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    [self.closeButton setTitle:@"Close" forState:UIControlStateNormal];
    
    self.imageView = [[UIImageView alloc] init];
    self.titleLabel = [[UILabel alloc] init];
    self.subtitleLabel = [[UILabel alloc] init];
    self.bottomSeperatorView = [[UIView alloc] init];
//    self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self addSubview:self.contentView];
    [self.contentView addSubview:self.closeButton];
    [self.contentView addSubview:self.imageView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.subtitleLabel];
    [self.contentView addSubview:self.bottomSeperatorView];
//    [self.contentView addSubview:self.cancelButton];
    [self.contentView addSubview:self.confirmButton];
    
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    self.subtitleLabel.numberOfLines = 0;
    self.titleLabel.textAlignment = self.subtitleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font = css.fontOfH1;
    self.titleLabel.textColor = css.color_3;
    
    self.subtitleLabel.font = css.fontOfH3;
    self.subtitleLabel.textColor = css.color_4;
    
//    self.cancelButton.titleLabel.font =
    self.confirmButton.titleLabel.font = css.fontOfH2;
//    [self.cancelButton setTitleColor:css.color_1 forState:UIControlStateNormal];
    [self.closeButton setImage:[UIImage imageNamed:@"common_banner_close"] forState:UIControlStateNormal];
    
    [self.confirmButton setTitleColor:css.color_1 forState:UIControlStateNormal];
    [self.confirmButton setTitle:@"购买" forState:UIControlStateNormal];
    self.titleLabel.text = @"付费专辑";
    self.subtitleLabel.text = @"该专辑为付费专辑视频，\n请购买后观看并配音";
    self.iconImage = [UIImage imageNamed:@"album_img_pay_bg"];
    self.imageView.image = self.iconImage;
    self.bottomSeperatorView.backgroundColor = css.color_7;
    
//    WEAKSELF

//    self.tapBgGesture = [[UITapGestureRecognizer alloc] initWithHandler:^(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint locationInView) {
//        [weakSelf dismissView];
//        
//        if (weakSelf.touchCloseButtonBlock) {
//            weakSelf.touchCloseButtonBlock();
//        }
//    }];
    
    [self.closeButton addTarget:self action:@selector(onCloseButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.confirmButton addTarget:self action:@selector(onConfirmButtonAction:) forControlEvents:UIControlEventTouchUpInside];

    [self layoutContentSubviews];
}


- (void)layoutContentSubviews {
    
    [self.contentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.mas_equalTo(260);
        
    }];
    
    CGFloat edgeSpace = 15;
    
    [self.closeButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(22);
        make.height.mas_equalTo(22);
        
        make.top.equalTo(self.contentView.mas_top).offset(10);
        make.trailing.equalTo(self.contentView.mas_trailing).offset(-10);
    }];
    
    if (self.iconImage) {
        [self.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(40);
            make.height.mas_equalTo(100);
            make.leading.equalTo(self.contentView).offset(edgeSpace);
            make.trailing.equalTo(self.contentView).offset(-edgeSpace);
        }];
    } else {
        [self.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(20);
            make.height.mas_equalTo(0);
            make.leading.equalTo(self.contentView).offset(edgeSpace);
            make.trailing.equalTo(self.contentView).offset(-edgeSpace);
        }];
    }
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imageView.mas_bottom).offset(20);
        make.height.mas_equalTo(25);
        make.leading.equalTo(self.contentView).offset(edgeSpace);
        make.trailing.equalTo(self.contentView).offset(-edgeSpace);
    }];
    
    [self.subtitleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(10);
//        make.height.mas_equalTo(25);
        make.leading.equalTo(self.contentView).offset(edgeSpace);
        make.trailing.equalTo(self.contentView).offset(-edgeSpace);
    }];
    
    [self.bottomSeperatorView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.top.equalTo(self.subtitleLabel.mas_bottom).offset(25);
        make.leading.equalTo(self.contentView);
        make.trailing.equalTo(self.contentView);
    }];
    
    [self.confirmButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(50);
        make.top.equalTo(self.bottomSeperatorView.mas_bottom).offset(0);
        make.bottom.equalTo(self.contentView.mas_bottom);
        make.leading.equalTo(self.contentView);
        make.trailing.equalTo(self.contentView);
    }];
    
}

- (void)onCloseButtonAction:(id)sender {
    [self dismissView];
    if (self.touchCloseButtonBlock) {
        self.touchCloseButtonBlock();
    }
}

- (void)onConfirmButtonAction:(id)sender {
    [self dismissView];
    if (self.touchConfirmButtonBlock) {
        self.touchConfirmButtonBlock();
    }
}

- (void)showInView:(UIView *)fromView {
    if (fromView) {
        [fromView addSubview:self];
        self.frame = fromView.bounds;
        self.hidden = NO;
        [self layoutContentSubviews];
    }
}

- (void)dismissView {
    self.hidden = YES;
    if ([self superview]) {
        [self removeFromSuperview];
    }
    
}

@end
